<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>员工信息 Employee Form</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/wang/wangeditor.css}">
    <style type="text/css">
        html{
            background-color: #FFF;
        }
        .pic {
            margin-left: 130px;
        }

        .pic dl {
            float: left;
            position: relative;
        }

        .pic dl dd {
            position: absolute;
            right: 5px;
            top: 5px;
            cursor: pointer;
            background: #666;
            color: #fff;
            padding: 2px;
        }

        .pic img {
            height: 100px;
            margin: 5px 0;
            margin-right: 5px;
        }
    </style>
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-form-ad" id="layuiadmin-form-ad" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入员工姓名"
                   class="layui-input" th:value="${employee == null ? '': employee.name}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-inline">
            <input type="text" name="photo" id="photo" lay-verify="required"  placeholder="请上传员工头像" class="layui-input" th:value="${employee == null ? '': employee.photo}">
        </div>
        <button type="button" class="layui-btn upload upload-pic" data-des="photo">
            <i class="layui-icon">&#xe67c;</i>上传头像
        </button>
        <div class="pic pic_box">
            <dl th:if="${employee != null && employee.photo != null}">
                <dt><img th:src="${employee.photo}"
                         th:data-url="${employee.photo}"></dt>
                <dd name="deleteFile" th:data-url="${employee.photo}"  data-des="picture">删除</dd>
            </dl>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">职位</label>
        <div class="layui-input-block">
            <input type="text" name="position" autocomplete="off" placeholder="请输入员工职位"
                   class="layui-input" th:value="${employee == null ? '': employee.position}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司名称</label>
        <div class="layui-input-block">
            <input type="text" name="company" lay-verify="required" autocomplete="off" placeholder="请输入员工公司名称"
                   class="layui-input" th:value="${employee == null ? '': employee.company}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea id="description" name="description" placeholder="请输入员工描述"  class="layui-textarea">[[${employee == null ? '': employee.description}]]</textarea>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="hidden" name="id" th:value="${employee == null ? 0 : employee.id}"  class="layui-input"/>
        <input type="hidden" name="factoryId" th:value="${employee == null ? factory.id : employee.factoryId}" class="layui-input"/>
        <input type="hidden" name="lang" th:value="${employee == null ? factory.lang : employee.lang}" class="layui-input"/>
        <button class="layui-btn" lay-submit lay-filter="LAY-employee-add-submit" id="LAY-employee-add-submit">提交</button>
    </div>
</div>

<script th:src="@{/statics/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'upload', 'form', 'wangeditor'], function () {
        var $ = layui.$
            , form = layui.form
            , wang = layui.wangeditor('description')
            , upload = layui.upload;

        wang.config.customUpload = false;
        wang.config.uploadImgUrl = '/web/wangEditor/local/saveImg';
        wang.config.uploadImgFileName = 'file';
        wang.config.uploadImgFns.onload = function (resultText, xhr) {
            var obj = eval('(' + resultText + ')');
            wang.command(null, 'insertHtml', '<img src="' + obj.data + '" style="max-width:100%;"/>');
        };
        wang.create();

        upload.render({
            elem: '.upload-pic'
            , url: '/api/file/local/upload'
            , accept: 'images'
            , method: 'post'
            , multiple: false //多文件上传
            , acceptMime: 'image/*'
            ,before: function(obj){
                layer.load(); //上传loading
            }
            ,done: function(res){
                var item = this.item;
                var des=$(item).data('des');
                console.log(des);
                layer.closeAll('loading'); //关闭loading
                if(res.code==0){
                    $('#'+des).val(res.data);
                    layer.msg('上传成功！');
                }else{
                    layer.msg('上传失败！');
                }
            }
            ,error: function(){
                layer.closeAll('loading'); //关闭loading
                layer.msg('上传发生错误！');
            }
        });

        $("dd[name='deleteFile']").on("click" , function(){
            var url = $(this).data('url');
            var des = $(this).data('des');
            var picbox = $(this).parent().parent();
            $.ajax({
                url: '/api/file/local/remove?key=' + url,
                method: 'DELETE',
                contentType: "application/json;charset=UTF-8",
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == '0') {
                        layer.msg('文件已删除!', {icon: 1, time: 1000}, function () {
                            picbox.hide();
                            $('#'+des).val('');
                        });
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function (err) {
                }
            });
        });

    })
</script>
</body>
</html>